package com.dudu.log.voiceLog.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.dudu.enumeration.AbilityTypeEnum;
import com.dudu.log.voiceLog.dao.VoiceDao;
import com.dudu.log.voiceLog.service.IVoiceService;

import core.CoreDao;
import core.CoreDaoInvoke;
import net.sf.json.JSONObject;

public class VoiceServiceImpl implements IVoiceService {

    private CoreDao coreDao = CoreDaoInvoke.getInstance();
    private VoiceDao voiceDao = new VoiceDao();

    @Override
    public Integer getTotalAcount(String paramSql) {
	return voiceDao.getTotalAcount(coreDao, paramSql);
    }

    @Override
    public List<JSONObject> queryAll(String paramSql, String pageSql) {
	List<JSONObject> list = voiceDao.queryAll(coreDao, paramSql, pageSql);
	if (null == list) {
	    return list;
	}
	for (JSONObject object : list) {
	    String ability_type = object.optString("voice_type");
	    AbilityTypeEnum ab = AbilityTypeEnum.getAbilityTypeEnum(ability_type);
	    String ability_typevalue = "";
	    if (ab != null) {
		ability_typevalue = ab.getValue();
	    }
	    if (StringUtils.isBlank(ability_typevalue)) {
		object.put("voice_type", ability_type);
	    } else {
		object.put("voice_type", ability_typevalue);
	    }
	}
	return list;
    }

    @Override
    public int resendVoiceById(String id) {
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	Date now = new Date();
	Date nowAdd = new Date(now.getTime() + 1 * 60 * 1000);
	String schedule_send_time = format.format(nowAdd);
	return voiceDao.resendVoiceById(coreDao, id, schedule_send_time);
    }

    @Override
    public int resendVoiceByParam(String param) {
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	Date now = new Date();
	Date nowAdd = new Date(now.getTime() + 1 * 60 * 1000);
	String schedule_send_time = format.format(nowAdd);
	return voiceDao.resendVoiceByParam(coreDao, param, schedule_send_time);
    }
}
